'use strict'

const EslintFriendlyFormater = require('eslint-friendly-formatter')
const path = require('path')
const hash = require('hash-sum')

function resolve (_path) {
  return path.resolve(__dirname, '..', _path)
}
// eslint-disable-next-line no-unused-vars
function genCacheConfig (id, partialIdentifier, configFiles) {
  const fs = require('fs')
  const cacheDirectory = resolve(`node_modules/.cache/${id}`)

  const variables = {
    partialIdentifier,
    'project': require('../package.json').version,
    'cache-loader': require('cache-loader/package.json').version,
    env: process.env.NODE_ENV,
    test: !!process.env.VUE_CLI_TEST
  }

  if (configFiles) {
    const readConfig = file => {
      const absolutePath = resolve(file)
      if (fs.existsSync(absolutePath)) {
        return fs.readFileSync(absolutePath, 'utf-8')
      }
    }
    if (!Array.isArray(configFiles)) {
      configFiles = [ configFiles ]
    }
    for (const file of configFiles) {
      const content = readConfig(file)
      if (content) {
        variables.configFiles = content
        break
      }
    }
  }

  const cacheIdentifier = hash(variables)
  return { cacheDirectory, cacheIdentifier }
}

module.exports = {
  context: process.pwd,
  module: {
    rules: [
      {
        test: /\.js$/,
        enforce: 'pre',
        exclude: [ /node_modules/ ],
        use: [
          {
            loader: 'eslint-loader',
            options: {
              formatter: EslintFriendlyFormater,
              cache: true,
              failOnWarning: true
            }
          }
        ]
      },
      {
        test: /\.js$/,
        exclude: [ /node_modules/ ],
        use: [
          // {
          //   loader: 'cache-loader',
          //   options: genCacheConfig('babel-loader', {
          //     'babel-loader': require('babel-loader/package.json').version,
          //     '@babel/core': require('@babel/core/package.json').version
          //   }, '.babelrc')
          // },
          'babel-loader'
        ]
      },
      // {
      //   test: /\.ts$/,
      //   enforce: 'pre',
      //   exclude: [ /node_modules/ ],
      //   use: [
      //     {
      //       loader: 'tslint-loader'
      //     }
      //   ]
      // },
      {
        test: /\.ts$/,
        exclude: [ /node_modules/ ],
        use: [
          // {
          //   loader: 'cache-loader',
          //   options: genCacheConfig('ts-loader', {
          //     'ts-loader': require('ts-loader/package.json').version,
          //     'typescript': require('typescript/package.json').version
          //   }, 'tsconfig.json')
          // },
          'babel-loader',
          {
            loader: 'ts-loader',
            options: {
              appendTsSuffixTo: [ '\\.vue$' ]
            }
          }
        ]
      },
      {
        test: /\.css$/,
        use: [
          'style-loader',
          'css-loader'
        ]
      },
      {
        test: /\.scss$/,
        use: [
          'style-loader',
          'css-loader',
          'sass-loader'
        ]
      },
      {
        test: /\.(png|svg|jpg|gif)$/,
        use: [
          {
            loader: 'url-loader',
            options: {
              limit: 8192
            }
          }
        ]
      },
      {
        test: /\.(woff|woff2|eot|ttf|otf)$/,
        use: [
          'file-loader'
        ]
      },
      {
        test: /\.(csv|tsv)$/,
        use: [
          'csv-loader'
        ]
      },
      {
        test: /\.xml$/,
        use: [
          'xml-loader'
        ]
      }
    ]
  },
  resolve: {
    // Add `.ts` and `.tsx` as a resolvable extension.
    extensions: [ '.ts', '.js', '.json' ]
  },
  node: {
    __dirname: process.env.NODE_ENV !== 'production',
    __filename: process.env.NODE_ENV !== 'production'
  }
}
